package com.gxkj.projects.myshopx.services.impl;

import com.gxkj.common.exceptions.ValidateException;
import com.gxkj.common.utils.ListPager;
import com.gxkj.common.utils.ValidatorUtil;
import com.gxkj.projects.myshopx.dao.UserRankDaoImpl;
import com.gxkj.projects.myshopx.entitys.User;
import com.gxkj.projects.myshopx.entitys.UserRank;
import com.gxkj.projects.myshopx.services.UserRankService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserRankServiceImpl   implements UserRankService {
    Logger LOG = LoggerFactory.getLogger(UserRankServiceImpl.class);


    @Autowired
    private UserRankDaoImpl userRankDao;

    @Autowired
    private ValidatorUtil validatorUtil;

    public void doAddUserRank(User user, UserRank entity) throws ValidateException {
        entity.setCreatedAt(new Date());
        entity.setUpdatedAt(new Date());
        entity.setIsDel(0);
        validatorUtil.validate(entity,true);

        userRankDao.insert(entity);
    }


    public void doUpdateUserRank(User user, UserRank entity) throws ValidateException {
        UserRank dbentity = this.getById(entity.getId());
        entity.setIsDel(dbentity.getIsDel());
        entity.setCreatedAt(dbentity.getCreatedAt());
        entity.setUpdatedAt(new Date());

        validatorUtil.validate(entity,true);

        userRankDao.updateByMerge(entity);
    }


    public void doDeleteUserRank(User user, String id) {
        UserRank dbentity = this.getById(id);
        dbentity.setIsDel(1);
        userRankDao.update(dbentity);
    }


    public ListPager<UserRank> doPageHQL(int pagenNo, int pageSize, UserRank entity) {
        return userRankDao.doPageHQL(pagenNo,pageSize,entity);
    }


    public UserRank getById(String id) {
        UserRank dbentity = userRankDao.selectById(id,UserRank.class);
        return dbentity;
    }
}
